#include("/common/_layout.html")
#@layout()
#define layoutHead()
	<title>内容编辑</title>
#end
#define layoutBody()
	<body>
		<div class="page-content-wrap">
				<form class="layui-form" lay-filter="formedit">
					<input type="hidden" name="article_id">
					<input type="hidden" name="org_id" value="0">
					<div class="layui-tab" style="margin: 0;">
						<ul class="layui-tab-title">
							<li><a id="a_goback_url" href="javascript:window.history.back();">内容列表</a></li>
							<li class="layui-this">内容详细</li>
						</ul>
						<div class="layui-tab-content">
							<div class="layui-tab-item"></div>
							<div class="layui-tab-item layui-show">
								<div class="layui-form-item">
									<label class="layui-form-label">栏目：</label>
									<div class="layui-input-block">
										<select id="cat_id" name="cat_id" lay-verify="required" disabled>
											<option value="">选择栏目</option>
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">标题：</label>
									<div class="layui-input-block">
										<input type="text" name="article_title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input" readonly>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">来源：</label>
									<div class="layui-input-block">
										<input type="text" name="source_name" placeholder="信息来源" autocomplete="off" class="layui-input" readonly>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">来源地址：</label>
									<div class="layui-input-block">
										<input type="text" name="source_url" placeholder="原文地址" autocomplete="off" class="layui-input" readonly>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">列表图片：</label>
									<div class="layui-input-block">
										<input type="hidden" id="list_pic_url" name="list_pic_url">
										<div id="div_list_pic_url" style="padding: 5px;"></div>
									</div>
								</div>
								<div class="layui-form-item" style="display: none;">
									<label class="layui-form-label">视频：</label>
									<div class="layui-input-block">
										<input type="hidden" id="video_url" name="video_url">
										<div id="div_video_url" style="padding: 10px;"></div>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">点击率：</label>
									<div class="layui-input-block">
										<input type="text" name="article_hit" placeholder="点击率" autocomplete="off" class="layui-input" readonly>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">详细内容：</label>
									<div class="layui-input-block">
										<textarea class="layui-textarea layui-hide" id="article_desc" name="article_desc" lay-verify="content"></textarea>
									</div>
								</div>
							</div>
						</div>
					</div>
				</form>
		</div>
		<script src="/static/js/jquery.formHelp.js" type="text/javascript" charset="utf-8"></script>
		<script>
			var url_cat_id = getUrlParam("cat_id");
			var article_desc_edit_idx;
			
			layui.use(['form', 'layer', 'laytpl', 'element', 'upload', 'layedit'], function() {
				var form = layui.form();
				var layer = layui.layer;
				var laytpl = layui.laytpl,
					layedit = layui.layedit,
					element = layui.element();
				
				function formatUpFile(elid, surl, ftype){
					if (surl == null || surl == "" || surl == "undefined"){
						return;
					}
					var shtml = "";
					if (ftype=="images"){
						shtml = '<a href="'+surl+'" target="_blank"><img src="'+surl+'" width="100" height="100"></a>';
					}else{
						shtml = '<a href="'+surl+'" target="_blank">'+surl.substr(surl.lastIndexOf('/')+1)+'</a>';
					}
					$('#div_'+elid).html(shtml);
				}
				
				function initDescEditor(){
					//创建一个编辑器
					article_desc_edit_idx = layedit.build('article_desc', {
						tool: [],
						height: 300
					});
				}
				
				// 表单验证
				form.verify({
				});

				
				// 初始化上级菜单
				function bindSelectData(listdata, pid, level){
					var rows = listdata.filter(function (e) { return e.parent_id == pid && (e.link_course === 0 || e.link_course === false); });
					for (var i = 0; i < rows.length; i++) {
						var shtml = '<option value="'+rows[i].cat_id+'">';
						for(j=0;j<level;j++){
							shtml += '&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						if (level>0){
							shtml += '├─';
						}
						shtml += rows[i].cat_name + '</option>';
						//console.log(shtml);
						document.getElementById('cat_id').innerHTML += shtml;
						// 迭代
						bindSelectData(listdata, rows[i].cat_id, level+1);
					}
				}

				// 页面初始化
				function page_init(){
					var id = getUrlParam('id');

					// 加载初始化编辑数据
					sendGet("/api/eduorg/article/getOne", {
						id : getUrlParam("id"),
						type_id : getUrlParam("typeid"),
					}, function(res) {
						//console.log(res);
						if (res.hasOwnProperty("listcat")) {
							bindSelectData(res.listcat, 0, 0);
							if (url_cat_id != null && url_cat_id != "undefined" && url_cat_id>0){
								$("#cat_id").val(url_cat_id);
							}
						}
						// 初始化编辑数据
						if (res.hasOwnProperty("oneinfo")) {
							$("form.layui-form").setForm(res.oneinfo);
							formatUpFile("list_pic_url", res.oneinfo.list_pic_url, "images");
							formatUpFile("video_url", res.oneinfo.video_url, "video");
							initDescEditor();
						}
						form.render();
					});
				}
				
				initDescEditor();
				page_init();
			});
			
			
					
		</script>
	</body>

#end